/*
 * @Author: 李无敌
 * @Date: 2025-07-25 17:01:56
 * @LastEditors: 李无敌
 * @LastEditTime: 2025-08-13 14:27:27
 * @FilePath: \nest-base\src\common\dto\response.dto.ts
 */
import { ApiProperty } from '@nestjs/swagger';
import { PaginationResponseDto } from './pagination.dto';
import { ResponseCode } from '../types/response-code.enum';

export class ApiResponseDto<T = any> {
  @ApiProperty({
    description: '响应状态码',
    example: '00000',
    type: 'string',
  })
  code: string;

  @ApiProperty({
    description: '响应消息',
    example: '操作成功',
  })
  message: string;

  @ApiProperty({
    description: '响应数据',
  })
  data?: T;

  @ApiProperty({
    description: '时间戳',
    example: 1704067200000,
    type: 'number',
  })
  timestamp: number;

  @ApiProperty({
    description: '请求路径',
    example: '/api/users',
    required: false,
  })
  path?: string;

  constructor(code: string, message: string, data?: T, path?: string) {
    this.code = code;
    this.message = message;
    this.data = data;
    this.timestamp = Date.now();
    this.path = path;
  }

  static success<T>(data?: T, message = '操作成功', path?: string): ApiResponseDto<T> {
    return new ApiResponseDto(ResponseCode.SUCCESS, message, data, path);
  }

  static error(
    code: Exclude<ResponseCode, ResponseCode.SUCCESS>,
    message?: string,
    path?: string
  ): ApiResponseDto {
    return new ApiResponseDto(code, message || '操作失败', undefined, path);
  }

  static badRequest(message = '请求参数错误', path?: string): ApiResponseDto {
    return new ApiResponseDto(ResponseCode.BAD_REQUEST, message, undefined, path);
  }

  static unauthorized(message = '未授权访问', path?: string): ApiResponseDto {
    return new ApiResponseDto(ResponseCode.UNAUTHORIZED, message, undefined, path);
  }

  static forbidden(message = '禁止访问', path?: string): ApiResponseDto {
    return new ApiResponseDto(ResponseCode.FORBIDDEN, message, undefined, path);
  }

  static notFound(message = '资源不存在', path?: string): ApiResponseDto {
    return new ApiResponseDto(ResponseCode.NOT_FOUND, message, undefined, path);
  }

  static validationError(message = '数据验证失败', path?: string): ApiResponseDto {
    return new ApiResponseDto(ResponseCode.VALIDATION_ERROR, message, undefined, path);
  }

  static businessError(message = '业务逻辑错误', path?: string): ApiResponseDto {
    return new ApiResponseDto(ResponseCode.BUSINESS_ERROR, message, undefined, path);
  }

  static internalError(message = '服务器内部错误', path?: string): ApiResponseDto {
    return new ApiResponseDto(ResponseCode.INTERNAL_SERVER_ERROR, message, undefined, path);
  }
}

export class PaginatedResponseDto<T> extends ApiResponseDto<PaginationResponseDto<T>> {
  constructor(data: PaginationResponseDto<T>, path?: string) {
    super(ResponseCode.SUCCESS, '查询成功', data, path);
  }
} 